package com.leruan.aasimp.action.maintenance;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.entity.CarCompanyInfo;
import com.leruan.aasimp.entity.CarOdd;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.service.MaintenanceService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * swb
 */
public class OddAction extends ActionSupport {

	private static final long serialVersionUID = -1906715812583393960L;
	
	private MaintenanceService maintenanceService;
	
	public String addOdd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Long oddId = Long.parseLong(request.getParameter("odd_id"));
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 
		Date createDate = null;
		Date oddDate = null;
		try {
			createDate = fmt.parse(request.getParameter("create_date"));
			oddDate = fmt.parse(request.getParameter("begintime"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String oddCompany = request.getParameter("company");
		String vin = request.getParameter("vin");
		String persontel = request.getParameter("tel");
		String personemail = request.getParameter("email");
		String remark = request.getParameter("remark");
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		CarOdd odd = new CarOdd();
		odd.setOddId(oddId);
		odd.setCreateDate(createDate);
		odd.setOddDate(oddDate);
		odd.setOddCompany(oddCompany);
		odd.setVin(vin);
		odd.setPersonname(eauser.getUsername());
		odd.setPersontel(persontel);
		odd.setPersonemail(personemail);
		odd.setRemark(remark);
		odd.setStatus("1");
		
		maintenanceService.addOdd(odd);
		request.setAttribute("message", "预约成功！");
		return getOdd();
	}
	public String getOdd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser==null){
			request.setAttribute("info", "您还没有登录，请先登录！");
			return "index";
		}
		List<CarOdd> odds = maintenanceService.getOdd(eauser.getUsername());
		
		JSONArray array = JSONArray.fromObject(odds);
		System.out.println(array.toString()); 
		request.setAttribute("odds", array.toString().replace("\"", "'"));
		return "odd";
	}
	public String deleteodd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
		.get(ServletActionContext.HTTP_REQUEST);
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		odd.setStatus("0");
		maintenanceService.updateOdd(odd);
		request.setAttribute("message", "删除成功！");
		return getOdd();
	}
	public String showodd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
		.get(ServletActionContext.HTTP_REQUEST);
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		request.setAttribute("oneOdd", odd);
		return "odd";
	}
	
	public String choiceodd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
		.get(ServletActionContext.HTTP_REQUEST);
		CarCompanyInfo companyInfo = (CarCompanyInfo) request.getSession().getAttribute("companyInfo");
		List<CarOdd> odds = maintenanceService.getPassOddByCompanyNm(companyInfo.getCompanyName());
		request.setAttribute("odds", odds);
		return "choiceodd";
	}

	public MaintenanceService getMaintenanceService() {
		return maintenanceService;
	}

	public void setMaintenanceService(MaintenanceService maintenanceService) {
		this.maintenanceService = maintenanceService;
	}

}
